package com.hu.zhcc.shiro.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import com.hu.zhcc.shiro.dao.MemberDAO;
import com.hu.zhcc.shiro.entity.bo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("memberService")
public class MemberServiceImpl implements MemberService{
    @Autowired
    MemberDAO memberDAO;

    @Override
    public PageInfo<Member> getMemberByName(Integer offset, Integer limit, String name) {
        PageHelper.startPage(offset,limit);
        List<Member> members = memberDAO.getMemberByName(name);
        PageInfo<Member> pageInfo = new PageInfo<>(members);

        return pageInfo;
    }

    @Override
    public PageInfo<Member> listAllMember(Integer offset, Integer limit) {
        PageHelper.startPage(offset,limit);
        List<Member> members = memberDAO.ListAllMember();
        PageInfo<Member> pageInfo = new PageInfo<>(members);

        return pageInfo;
    }

    @Override
    public Member getMember(Integer id) {
        Member member = memberDAO.getMember(id);
        return member;
    }

    @Override
    public int updateMember(Member member) {
        int i = memberDAO.updateByPrimaryKeySelective(member);
        return i;
    }

    @Override
    public int addMember(Member member) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        member.setRegTime(formatter.format(new Date()));
        int i = memberDAO.insertSelective(member);
        return i;
    }

    @Override
    public Member getMemberByPhoneNumber(String phone, String pwd) {
        Member member = memberDAO.getMemberByPhoneNumber(phone);
        if (member != null && member.getPassword().equals(pwd)) {
            return member;
        }
        return null;
    }

    @Override
    public int deleteMember(Integer id) {
        int i = memberDAO.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public Member getMember(String IdCard) {
        Member member = memberDAO.getMemberByIdCard(IdCard);
        return member;
    }
}
